﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Microsoft.WVB.Silverlight.Infrastructure;
using Microsoft.WVB.UI;

namespace Microsoft.WVB
{
    public partial class Main : UserControl
    {
        ILayoutManager manager = null;
        public Main()
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                manager = IoC.GetCreate<ILayoutManager>();
                ViewModel = IoC.GetCreate<MainModel>();
                DataContext = ViewModel;
            }
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Main_Loaded);

           // manager.RegisterMainCanvas(this.mainCanvas);
            Application.Current.Host.Content.FullScreenChanged += new System.EventHandler(Content_FullScreenChanged);
        }

        private void Main_Loaded(object sender, RoutedEventArgs e)
        {
            this.channelBar.MouseEnter += new System.Windows.Input.MouseEventHandler(channelBar_MouseEnter);
            this.channelBar.MouseLeave += new System.Windows.Input.MouseEventHandler(channelBar_MouseLeave);

            CarouselModel ViewModel = IoC.Create<CarouselModel>();

            Storyboard dropawayOnStartup = this.LayoutRoot.Resources["channelBar_PopDown"] as Storyboard;

            // Note this is the same animation used in channelBar_MouseLeave. Just using a different BeginTime on the Storyboard
            dropawayOnStartup.BeginTime = TimeSpan.FromSeconds(5);
     
            dropawayOnStartup.Begin();
        }

        void channelBar_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Storyboard popDownStoryboard = this.LayoutRoot.Resources["channelBar_PopDown"] as Storyboard; //Application.Current.Resources["channelBar_PopDown"] as Storyboard;

            popDownStoryboard.BeginTime = TimeSpan.FromSeconds(1);

            // Begin the animation.
            popDownStoryboard.Begin();
        }

        void channelBar_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Storyboard popUpStoryboard = this.LayoutRoot.Resources["channelBar_PopUp"] as Storyboard; // Application.Current.Resources[""] as Storyboard;

            // Begin the animation.
            popUpStoryboard.Begin();
        }

        void Content_FullScreenChanged(object sender, System.EventArgs e)
        {
            UIElement uiAdsPlayerPanel = this.FindName("AdsPlayerPanel") as UIElement;
            if (Application.Current.Host.Content.IsFullScreen)
            {
                ILayoutManager LayoutMgr =  IoC.GetCreate<ILayoutManager>();
                //LayoutMgr.MinimizeAllWindows();
                //uiAdsPlayerPanel.Visibility = Visibility.Visible;

                LayoutMgr.MaximiseControl(uiAdsPlayerPanel as IManagedControl);
                UpdateLayout();
            }
            else
            {
                ILayoutManager LayoutMgr = IoC.GetCreate<ILayoutManager>();
                //LayoutMgr.RestoreAllWindows();

                LayoutMgr.RestoreControl(uiAdsPlayerPanel as IManagedControl);
                UpdateLayout();
            }
        }

        /// <summary>
        /// Gets the controller for this view 
        /// </summary>
        public MainModel ViewModel { get; private set; }
    }
}
